﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.Remoting;
using Zone;
using System.Xml;
namespace sendcloud桌面版
{
    public partial class Form1 : BaseForm
    {
        DateTime dt1 = new DateTime();
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            base.CheckNet(this, dt1);
            dt1 = Daytime.GetStandardTime();
            addmm();
            label9.Text = "注意：如果使用邮件列表发送,则发件人请留空！";
        }
        private void button1_Click(object sender, EventArgs e)
        {
            SendCloud sc = new SendCloud();
            sc.Api_User = AppConfig.ConfigGetValue("app", "api_user");
            sc.Api_key = AppConfig.ConfigGetValue("app", "api_key");
            sc.From = AppConfig.ConfigGetValue("app", "from");
            sc.FromName = AppConfig.ConfigGetValue("app", "fromname");

            string s1 = richTextBox1.Text;
            string s3 = textBox3.Text;
            string s4 = richTextBox3.Text;
            string s11 = textBox6.Text;
            string sr = "";
            try
            {
                string s = sc.WebAPISendMail(s1, s3, s4, s11).Replace("\r\n", "");
                s = GetContent(s);
                if (s.Contains("success"))
                {
                    MessageBox.Show(s + "提交成功,请转至sendcloud平台查看发送状态！");
                }
                else
                {
                    MessageBox.Show("发送失败:" + s);
                }
                sr += s1 + "\t";
                sr += s3 + "\t";
                sr += s4 + "\t";

                base.WriteTxt(sr + s, "log", true);
            }
            catch (Exception)
            {
                throw;
            }
        }

        void MenuClicked(object sender, EventArgs e)
        {
            string s = ((sender as ToolStripMenuItem).Tag).ToString();
            if (s.Contains("参数设置"))
            {
                ObjectHandle t = Activator.CreateInstance("sendcloud桌面版", "sendcloud桌面版.SetPara");
                Form f = (Form)t.Unwrap();
                f.ShowDialog();
            }
            if (s.Contains("日志查看"))
            {
                System.Diagnostics.Process.Start("log.txt");
            }
            if (s.Contains("关于SendCloud桌面版"))
            {
                ObjectHandle t = Activator.CreateInstance("sendcloud桌面版", "sendcloud桌面版.UseAge");
                Form f = (Form)t.Unwrap();
                f.ShowDialog();
            }
            if (s.Contains("联系我们"))
            {
                ObjectHandle t = Activator.CreateInstance("sendcloud桌面版", "sendcloud桌面版.ContactUs");
                Form f = (Form)t.Unwrap();
                f.ShowDialog();
            }
        }

        ToolStripMenuItem AddContextMenu(string text, ToolStripItemCollection cms, EventHandler callback)
        {
            if (text == "-")
            {
                ToolStripSeparator tsp = new ToolStripSeparator();
                cms.Add(tsp);
                return null;
            }
            else if (!string.IsNullOrEmpty(text))
            {
                ToolStripMenuItem tsmi = new ToolStripMenuItem(text);
                tsmi.Tag = text + "TAG";
                if (callback != null) tsmi.Click += callback;
                cms.Add(tsmi);

                return tsmi;
            }

            return null;
        }


        void addmm()
        {
            //添加菜单一 
            ToolStripMenuItem subItem;
            subItem = AddContextMenu("设置(S)", menuStrip1.Items, null);
            //添加子菜单 
            AddContextMenu("参数设置(P)", subItem.DropDownItems, new EventHandler(MenuClicked));
            AddContextMenu("日志查看(L))", subItem.DropDownItems, new EventHandler(MenuClicked));

            //添加菜单二 
            subItem = AddContextMenu("帮助(H)", menuStrip1.Items, null);
            //添加子菜单 
            AddContextMenu("关于SendCloud桌面版(A)", subItem.DropDownItems, new EventHandler(MenuClicked));
            AddContextMenu("联系我们(C)", subItem.DropDownItems, new EventHandler(MenuClicked));
        }

        private void button2_Click(object sender, EventArgs e)
        {
            string s1 = textBox1.Text;
            string s2 = textBox2.Text;
            string s4 = textBox4.Text;
            string s5 = textBox5.Text;
            string sr = "";
            try
            {
                SendCloud sc = new SendCloud();
                sc.Api_User = AppConfig.ConfigGetValue("app", "api_user");
                sc.Api_key = AppConfig.ConfigGetValue("app", "api_key");
                sc.From = AppConfig.ConfigGetValue("app", "from");
                sc.FromName = AppConfig.ConfigGetValue("app", "fromname");

                string s = sc.WebAPISendMail_template(s2, s4, s1, s5).Replace("\r\n", "");
                s = GetContent(s);
                if (s.Contains("success"))
                {
                    MessageBox.Show(s + "提交成功,请转至sendcloud平台查看发送状态！");
                }
                else
                {
                    MessageBox.Show(s + "发送失败！");
                }
                sr += s1 + "\t";
                sr += s2 + "\t";
                sr += s4 + "\t";
                sr += s5 + "\t";
                base.WriteTxt(sr + s, "log", true);
            }
            catch (Exception)
            {
                throw;
            }
        }
        private string GetContent(string sss)
        {
            string s = "";
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.LoadXml(sss);
            XmlNodeList nodeList = xmlDoc.SelectSingleNode("result").ChildNodes;//获取bookstore节点的所有子节点
            //foreach (XmlNode xn in nodeList)//遍历所有子节点
            //{
            //    XmlText xe = (XmlText)xn;//将子节点类型转换为XmlElement类型

            //        XmlNodeList nls = xe.ChildNodes;//继续获取xe子节点的所有子节点
            //        foreach (XmlNode xn1 in nls)//遍历
            //        {
            //            XmlElement xe2 = (XmlElement)xn1;//转换类型
            //            if (xe2.Name == "error")//如果找到
            //            {
            //                s = xe2.InnerXml;
            //                break;//找到退出来就可以了
            //            }
            //        }
            //        break;               
            //}
            foreach (XmlNode xn in nodeList)//遍历所有子节点 
            {
                XmlElement xe = xn as XmlElement;//最好不好用强制转换，建议使用as，这样如果转不过去话，就是null，不会报异常
                if (xe != null)   //这样判断以下是否为null，或者你可以if（xe==null） return ；直接返回或做其他处理
                {                    
                    string Keyname = xe.Name;
                    if (Keyname == "message")//如果找到
                    {
                        s = xe.InnerText;
                        if (s == "success") break;
                    }
                    if (Keyname == "error")//如果找到
                    {
                        s = xe.InnerText;
                    }
                    else
                    {
                        XmlNodeList nls = xe.ChildNodes;
                        foreach (XmlNode xn2 in nls)//遍历所有子节点 
                        {
                            XmlElement xe2 = xn2 as XmlElement;//最好不好用强制转换，建议使用as，这样如果转不过去话，就是null，不会报异常
                            if (xe2 != null)   //这样判断以下是否为null，或者你可以if（xe==null） return ；直接返回或做其他处理
                            {
                                string Keyname2 = xe2.Name;
                                if (Keyname2 == "error")//如果找到
                                {
                                    s = xe.InnerText;
                                }                               
                            }
                        }
                    }
                }
            }
            return s;
        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            base.Initialize(dt1.AddMonths(2));
        }

        private void textBox6_TextChanged(object sender, EventArgs e)
        {
            richTextBox1.Text = "";
        }

        private void richTextBox1_TextChanged(object sender, EventArgs e)
        {
            textBox6.Text = "";
        }        
    }  
}
